<template>
    <div>
        <el-form ref="form" :rules="form_rules" :model="form" label-width="100px" size="small">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="项目编码" prop="project_code">
                        <el-input v-model="form.project_code"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="项目名称" prop="project_name">
                        <el-input v-model="form.project_name"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="占地面积">
                        <el-input v-model="form.total_area" @keyup.native="onkeyupPrice($event)" placeholder="数字，小数点两位，单位平方米">
                            <template slot="append">平方米</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="建筑面积">
                        <el-input v-model="form.built_area" @keyup.native="onkeyupPrice($event)" placeholder="数字，小数点两位，单位平方米">
                            <template slot="append">平方米</template>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="绿地面积">
                        <el-input v-model="form.green_space" @keyup.native="onkeyupPrice($event)" placeholder="数字，小数点两位，单位平方米">
                            <template slot="append">平方米</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="道路面积">
                        <el-input v-model="form.road_space" @keyup.native="onkeyupPrice($event)" placeholder="数字，小数点两位，单位平方米">
                            <template slot="append">平方米</template>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="楼宇数量" prop="building_amount">
                        <el-input v-model="form.building_amount" @keyup.native="onkeyupPlusInt($event)" placeholder="正整数"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="负责人">
                        <el-input v-model="form.charge"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="项目地址">
                        <el-input v-model="form.project_addr"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="公司名称">
                        <el-input v-model="form.dev_company"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="法人代表">
                        <el-input v-model="form.dev_legal"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="联系人">
                        <el-input v-model="form.dev_contact"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="联系电话">
                        <el-input v-model="form.dev_mobile"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="联系地址">
                        <el-input v-model="form.dev_addr"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="备注">
                        <el-input type="textarea" v-model="form.remark"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item>
                <el-button type="primary" @click="submit()">保存</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    data () {
        return {
            form: {
                project_code: '', // 项目编码
                project_name: '', // 项目名称
                total_area: '', // 占地面积
                built_area: '', // 建筑面积
                green_space: '', // 绿地面积
                road_space: '', // 道路面积
                building_amount: '', // 楼宇数量
                charge: '', // 负责人
                project_addr: '', // 项目地址
                dev_company: '', // 开发商 公司名称
                dev_legal: '', // 开发商 法人代表
                dev_contact: '', // 开发商 联系人
                dev_mobile: '', // 开发商 联系电话
                dev_addr: '', // 开发商 联系地址
                remark: '', // 开发商 备注
            },
            form_rules: {
                project_code: [
                    { required: true, message: '项目编码不能为空', trigger: 'blur'}
                ],
                project_name: [
                    { required: true, message: '项目名称不能为空', trigger: 'blur'}
                ],
                building_amount: [
                    { required: true, message: '楼宇数量不能为空', trigger: 'blur'}
                ]
            }
        }
    },
    props: {
        id: {
            type: Number
        }
    },
    created() {
        if (this.id) { // 编辑
            this.getOldData();
        }
    },
    methods: {
        getOldData() { // 获取编辑数据

            this.$https.get('/api/pms_base_projects/GetProjectDetail?project_id=' + this.id).then((result) => {
                if (result.data.code == 0) {
                    let _data = result.data.data;
                    this.form.project_code = _data.project_code; // 项目编码
                    this.form.project_name = _data.project_name; // 项目名称
                    this.form.total_area = _data.total_area == 0 ? '' : _data.total_area; // 占地面积
                    this.form.built_area = _data.built_area == 0 ? '' : _data.built_area; // 建筑面积
                    this.form.green_space = _data.green_space == 0 ? '' : _data.green_space; // 绿地面积
                    this.form.road_space = _data.road_space == 0 ? '' : _data.road_space; // 道路面积
                    this.form.building_amount = _data.building_amount; // 楼宇数量
                    this.form.charge = _data.charge; // 负责人
                    this.form.project_addr = _data.project_addr; // 项目地址
                    this.form.dev_company = _data.dev_company; // 开发商 公司名称
                    this.form.dev_legal = _data.dev_legal; // 开发商 法人代表
                    this.form.dev_contact = _data.dev_contact; // 开发商 联系人
                    this.form.dev_mobile = _data.dev_mobile; // 开发商 联系电话
                    this.form.dev_addr = _data.dev_addr; // 开发商 联系地址
                    this.form.remark = _data.remark; // 开发商 备注
                } else {
                    this.$message({
                        type: 'error',
                        showClose: true,
                        message: result.data.message
                    })
                }
            })
        },
        submit() { // 提交
            this.$refs.form.validate((valid) => {
                if (valid) {
                    let _postData = JSON.parse(JSON.stringify(this.form));
                    _postData.total_area = this.form.total_area == '' ? 0 : this.form.total_area; // 占地面积
                    _postData.built_area = this.form.built_area == '' ? 0 : this.form.built_area; // 建筑面积
                    _postData.green_space = this.form.green_space == '' ? 0 : this.form.green_space; // 绿地面积
                    _postData.road_space = this.form.road_space == '' ? 0 : this.form.road_space;
                    console.info(_postData);
                    if (this.id) { // 编辑
                        _postData.project_id = parseInt(this.id);
                        this.$https.post('/api/pms_base_projects/UpdateProject', _postData).then((result) => {
                            if (result.data.code == 0) {
                                this.$message({
                                    type: 'success',
                                    showClose: true,
                                    message: '编辑成功',
                                    duration: 1500,
                                    onClose: () => {
                                        // 关闭当前dialog，
                                        this.closeDialog('dialogProjectEdit');
                                        // 刷新列表
                                        this.$emit('parentGetDataList');
                                    }
                                })
                            } else {
                                this.$message({
                                    type: 'error',
                                    showClose: true,
                                    message: result.data.message
                                })
                            }
                        })
                    } else { // 新增

                        this.$https.post('/api/pms_base_projects/AppendProject', _postData).then((result) => {
                            if (result.data.code == 0) {
                                this.$message({
                                    type: 'success',
                                    showClose: true,
                                    message: '添加成功',
                                    duration: 1500,
                                    onClose: () => {
                                        // 关闭当前dialog，
                                        this.closeDialog('dialogProjectAdd');
                                        // 刷新列表
                                        this.$emit('parentGetDataList');
                                    }
                                })
                            } else {
                                this.$message({
                                    type: 'error',
                                    showClose: true,
                                    message: result.data.message
                                })
                            }
                        })
                    }
                    

                } else {
                    console.error('验证失败');
                    return false;
                }
            });
        },
        closeDialog(name) { // 关闭当前
            // this.$refs.form.resetFields(); // 重置表单
            if (!name) {
                this.$emit('closeDialog', 'dialogProjectAdd'); // 执行父组件关闭方法
                this.$emit('closeDialog', 'dialogProjectEdit'); // 执行父组件关闭方法
            } else {
                this.$emit('closeDialog', name); // 执行父组件关闭方法
            }
        }
    }
}
</script>
<style scoped>
</style>